package com.suyuan.controller.shop;

import com.alibaba.fastjson.JSON;
import com.suyuan.entity.CustomAddr;
import com.suyuan.entity.CustomInfo;
import com.suyuan.params.LoginParam;
import com.suyuan.params.RegistParam;
import com.suyuan.service.CustomerService;
import com.suyuan.utils.JsonMsg;
import com.suyuan.utils.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.util.List;

/**
 * 用户
 */
@Controller
@RequestMapping(value = "/shop/customer")
public class CustomerController {
    @Resource(name = "customerService")
    private CustomerService customerService;

    /**
     * 登录
     * 已测试
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public Object login(@RequestBody String data, HttpServletRequest request){

        System.out.println("login:"+data);
        LoginParam param = JsonUtils.jsonToPojo(data, LoginParam.class);
        //String correctCode = (String) request.getSession().getAttribute("checkcode");

        if(StringUtils.isBlank(param.getUsername()) || StringUtils.isBlank(param.getPassword())){
            return JsonMsg.ErrorMsg("参数不能为空");
        }

        /*if(StringUtils.isBlank(correctCode) || StringUtils.isBlank(checkcode)){
            return JsonMsg.ErrorMsg("验证码不能为空");
        }
        if(!correctCode.equalsIgnoreCase(checkcode)){
            return JsonMsg.ErrorMsg("验证码错误");
        }*/

        CustomInfo customer = customerService.findByUsername(param.getUsername());
        if(customer == null){
            return JsonMsg.ErrorMsg("用户不存在，请输入正确的用户名");
        }
        if(customer.getPassword().equals(param.getPassword())) {
            return JsonMsg.SuccessMsg(customer);
        }
        return JsonMsg.ErrorMsg("账号或密码错误");
    }


    /**
     *  根据id获取个人信息
     *  已测试
     */
    @RequestMapping(value = "/customerDetail")
    @ResponseBody
    public Object customerDetail(@RequestBody String data){
        CustomInfo customer = JsonUtils.jsonToPojo(data,CustomInfo.class);
        if(customer.getGid() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        CustomInfo customInfo = customerService.findById(customer.getGid());
        if(customInfo != null){
            return JsonMsg.SuccessMsg(customInfo);
        }
        return JsonMsg.ErrorMsg("用户不存在");
    }

    /**
     * 根据用户id查询其所有地址
     * 已测试
     * @return
     */
    @RequestMapping(value = "/findAddrById")
    @ResponseBody
    public Object findAddrById(@RequestBody String data){
        CustomAddr addr = JsonUtils.jsonToPojo(data, CustomAddr.class);
        if(addr.getId() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        List<CustomAddr> addrs = customerService.findAddrById(addr.getId());
        return JsonMsg.SuccessMsg(addrs);
    }

    /**
     * 添加新的地址
     * @return
     */
    @RequestMapping(value = "/newAddr")
    @ResponseBody
    public Object newAddr(@RequestBody String data){
        CustomAddr addr = JsonUtils.jsonToPojo(data, CustomAddr.class);
        System.out.println("addr cusId:"+ addr.getCusId());
        if(StringUtils.isBlank(addr.getAddr()) || StringUtils.isBlank(addr.getReceName()) || StringUtils.isBlank(addr.getRecePhone())){
            return JsonMsg.ErrorMsg("收货人、地址、联系方式不能为空");
        }
        boolean isSuccess = false;
        isSuccess = customerService.newAddr(addr);
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
        return JsonMsg.ErrorMsg("添加失败");
    }

    /**
     * 更新地址
     */
    @RequestMapping(value = "/updateAddr")
    @ResponseBody
    public Object updateAddr(@RequestBody String data){
        System.out.println("update addr:"+data);
        CustomAddr addr = JsonUtils.jsonToPojo(data, CustomAddr.class);
        if(addr == null){
            return JsonMsg.ErrorMsg("错误参数");
        }
        if(addr.getId() == null || StringUtils.isBlank(addr.getAddr()) || StringUtils.isBlank(addr.getReceName()) || StringUtils.isBlank(addr.getRecePhone())){
            return JsonMsg.ErrorMsg("收货人、地址、联系方式不能为空");
        }
        boolean isSuccess = false;
        isSuccess = customerService.updateAddr(addr);
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
        return JsonMsg.ErrorMsg("更新失败");
    }

    /**
     * 删除地址
     */
    @RequestMapping("/deleteAddr")
    @ResponseBody
    public Object deleteAddr(@RequestBody String data){

        System.out.println("delete addr:"+data);

        CustomAddr addr = JsonUtils.jsonToPojo(data, CustomAddr.class);
        if(addr.getId() == null){
            return JsonMsg.ErrorMsg("地址id不能为空");
        }
        boolean isSuccess = false;
        isSuccess = customerService.deleteAddr(addr.getId());
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
        return JsonMsg.ErrorMsg("删除失败");
    }


 /*   *//**
     * 登录
     * 已测试
     *//*
    @RequestMapping(value = "/login")
    @ResponseBody
    public Object login(@RequestBody String data, HttpServletRequest request){
        LoginParam param = JsonUtils.jsonToPojo(data, LoginParam.class);
        String correctCode = (String) request.getSession().getAttribute("checkcode");

        if(StringUtils.isBlank(param.getUsername()) || StringUtils.isBlank(param.getPassword())){
            return JsonMsg.ErrorMsg("参数不能为空");
        }

        if(StringUtils.isBlank(correctCode) || StringUtils.isBlank(param.getCheckcode())){
            return JsonMsg.ErrorMsg("验证码不能为空");
        }
        if(!correctCode.equalsIgnoreCase(param.getCheckcode())){
            return JsonMsg.ErrorMsg("验证码错误");
        }

        CustomInfo customer = customerService.findByUsername(param.getUsername());
        if(customer == null){
            return JsonMsg.ErrorMsg("用户不存在，请输入正确的用户名");
        }
        if(customer.getPassword().equals(param.getPassword())) {
            return JsonMsg.SuccessMsg(customer);
        }
        return JsonMsg.ErrorMsg("账号或密码错误");
    }*/


    /**
     *  注册
     * @return
     */
    @RequestMapping("/regist")
    @ResponseBody
    public Object regist(@RequestBody String data,HttpServletRequest request){
        //注册时为客户生成购物车对象
        try {
            RegistParam param = JsonUtils.jsonToPojo(data,RegistParam.class);
            customerService.registCustomer(param);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg("注册失败");
        }
        return JsonMsg.SuccessMsg("注册成功！请登录");
    }


    /**
     *  前台： 注册AJAX校验用户名
     * @throws
     */
    @RequestMapping(value = "checkUsername")
    public Object checkUserName(@RequestBody String data,HttpServletRequest request) {
        RegistParam param = JsonUtils.jsonToPojo(data,RegistParam.class);
        String username = (String) request.getSession().getAttribute(param.getUsername());
        CustomInfo customInfo = customerService.findByUsername(username);
        if(customInfo != null){
            return JsonMsg.ErrorMsg("账号已存在，请重新输入");
        }else{
            return JsonMsg.SuccessMsg("账号可用");
        }
    }




}
